<template>
  <div class="reg">
    <div class="box">
      <div class="header">
        <img src="@/assets/login_title.png" alt="" />
      </div>
      <div class="body">
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
              v-model="form.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              v-model="form.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item prop="repassword">
            <el-input
              placeholder="请再次输入密码"
              prefix-icon="el-icon-lock"
              v-model="form.repassword"
              show-password
            ></el-input>
          </el-form-item>
          <el-button
            type="primary"
            :round="true"
            class="btn"
            @click="reg()"
            :loading="loading"
            >注册</el-button
          >
          <router-link to="/login" class="go_login"
            >以有账号?去登陆</router-link
          >
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { Reg } from '@/api/reg.js'
export default {
  data () {
    return {
      form: {
        username: '',
        password: '',
        repassword: ''
      },
      loading: false,
      // /^[a-zA-Z0-9_-]{4,16}$/
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              //密码 非空字符串、长度 6-15
              const reg = /^[a-zA-Z0-9]{1,10}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('用户名必须字母数字组成、长度 1-10'))
              }
            },
            trigger: 'change'
          }
        ],
        //非空字符串、长度 6-15
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              //用户名正则，1到10位（字母，数字）
              const reg = /^[a-zA-Z0-9]{6,15}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('密码必须字母数字组成、长度 6-15'))
              }
            },
            trigger: 'change'
          }
        ],
        //非空字符串、长度 6-15
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (value === this.form.password) {
                callback()
              } else {
                callback(new Error('两次密码不一样'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    reg () {
      this.$refs.form.validate(result => {
        this.loading = true
        if (result) {
          Reg(this.form)
            .then(res => {
              // console.log(res.data)
              this.$message.success(res.data.message)
              this.$router.push('/login')
            })
            .catch(error => {
              // console.log(error)
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reg {
  height: 100%;
  background: url(~@/assets/login_bg.jpg) center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 400px;
    // height: 400px;
    background-color: #fff;
    padding: 0 40px;
    .header {
      text-align: center;
      padding: 20px 0;
    }
    .body {
      text-align: center;
      font-size: 12px;
      .btn {
        width: 100%;
      }
      .go_login {
        line-height: 50px;
      }
    }
  }
}
</style>
